﻿package frEngine.primitives.lightingline
{

    public class FrNoise extends Object
    {
        private var _frequency:Number;
        private var _seed:int;
        private var _factorScale:Number;
        private var _factorAdd:Number;

        public function FrNoise(param1:Number = 0, param2:Number = 1, param3:Number = 1, param4:int = 0)
        {
            this._seed = param4;
            this._frequency = param3;
            this._factorScale = (param2 - param1) * 0.5;
            this._factorAdd = (param2 + param1) * 0.5;
            return;
        }// end function

        public function generateNoise1D(param1:Number) : Number
        {
            param1 = param1 * this._frequency;
            var _loc_2:* = int(param1);
            var _loc_3:* = param1 - _loc_2;
            _loc_2 = _loc_2 + this._seed;
            var _loc_4:* = this.noise1d(_loc_2);
            var _loc_5:* = this.noise1d((_loc_2 + 1));
            return this.interpolate(_loc_4, _loc_5, _loc_3) * this._factorScale + this._factorAdd;
        }// end function

        public function generateNoise2D(param1:Number, param2:Number) : Number
        {
            param1 = param1 * this._frequency;
            param2 = param2 * this._frequency;
            var _loc_3:* = int(param1);
            var _loc_4:* = int(param2);
            var _loc_5:* = param1 - _loc_3;
            var _loc_6:* = param2 - _loc_4;
            _loc_3 = _loc_3 + this._seed;
            _loc_4 = _loc_4 + this._seed;
            var _loc_7:* = this.noise2d(_loc_3, _loc_4);
            var _loc_8:* = this.noise2d((_loc_3 + 1), _loc_4);
            var _loc_9:* = this.noise2d(_loc_3, (_loc_4 + 1));
            var _loc_10:* = this.noise2d((_loc_3 + 1), (_loc_4 + 1));
            var _loc_11:* = this.interpolate(_loc_7, _loc_8, _loc_5);
            var _loc_12:* = this.interpolate(_loc_9, _loc_10, _loc_5);
            return this.interpolate(_loc_11, _loc_12, _loc_6) * this._factorScale + this._factorAdd;
        }// end function

        private function noise1d(param1:int) : Number
        {
            param1 = param1 << 13 ^ param1;
            var _loc_2:* = param1 * param1;
            var _loc_3:* = _loc_2 * 15731 + 789221;
            var _loc_4:* = param1 * _loc_3;
            var _loc_5:* = _loc_4 + 1376312589;
            var _loc_6:* = _loc_5 & 2147483647;
            return 1 - Number(_loc_6) * 9.31323e-010;
        }// end function

        private function noise2d(param1:int, param2:int) : Number
        {
            var _loc_3:* = param1 + param2 * 57;
            return this.noise1d(_loc_3);
        }// end function

        private function interpolate(param1:Number, param2:Number, param3:Number) : Number
        {
            var _loc_4:* = param3 * 3.14159;
            var _loc_5:* = (1 - Math.cos(_loc_4)) * 0.5;
            return param1 * (1 - _loc_5) + param2 * _loc_5;
        }// end function

    }
}
